package com.esms.struts.service;

public class JspWidget{
	
	private JspWidget() {}
	public static JspWidget $ = new JspWidget(); 
	
	public String actionLink(String label, String url, Object[] pars) {
		return actionLinks(label, url, pars, null);
	}
	
	public String actionLink(String label, String url, Object[] pars, String destDiv) {
		return actionLinks(label, url, pars, destDiv);
	}
	
	private String actionLinks(String label, String url, Object[] pars, String destDiv) {
		StringBuffer sb = new StringBuffer("");
		sb.append("<a href=\"javascript:;\" onclick=\"var myAjax = new Ajax.Updater('");
		if(null == destDiv)
			sb.append("right");
		else 
			sb.append(destDiv);
		sb.append("','");
		sb.append(url);
		sb.append("' ,{method:'get', parameters:" );
		if(pars != null && pars.length > 1) {
			for(int i = 0; i < pars.length; i++) {
				if(i == pars.length - 1)
					sb.append(pars[i]);
				else {
					sb.append(pars[i]);
					sb.append("&");
				}
			}
		} else if(pars != null && pars.length == 1){
			sb.append(pars[0]);
		} else {
			sb.append("''");
		}
		sb.append(",evalScripts: true});\">");
		sb.append(label);
		sb.append("</a>");

		return sb.toString();
	}
	
	public String actionButton(String label, String url, Object[] pars) {
		return actionButtons(label, url, pars, null);
	}
	
	public String actionButton(String label, String url, Object[] pars, String destDiv) {
		return actionButtons(label, url, pars, destDiv);
	}
	
	private String actionButtons(String label, String url, Object[] pars, String destDiv) {
		StringBuffer sb = new StringBuffer("");
		sb.append("<input type=\"button\" onclick=\"var myAjax = new Ajax.Updater('");
		if(null == destDiv)
			sb.append("right");
		else 
			sb.append(destDiv);
		sb.append("','");
		sb.append(url);
		sb.append("' ,{method:'get', parameters:" );
		if(pars != null && pars.length > 1) {
			for(int i = 0; i < pars.length; i++) {
				if(i == pars.length - 1)
					sb.append(pars[i]);
				else {
					sb.append(pars[i]);
					sb.append("&");
				}
			}
		} else if(pars != null && pars.length == 1){
			sb.append(pars[0]);
		} else {
			sb.append("''");
		}
		sb.append(",evalScripts: true});\" value=\""+ label +"\"/>");

		return sb.toString();
	}
}
